<template>
  <div class="dialog">
    <el-dialog width="45%" title="增加数量" :visible="true" :close-on-click-modal="false" :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="新增数量：" prop="number">
          <el-col :span="12">
            <el-input v-model="form.num" v-int placeholder="请输入新增数量" clearable />
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" :loading="loading" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { addCouponNumber } from '@/api/coupon'
export default {
  name: 'AddNumber',
  props: {
    id: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      form: {
        num: ''
      },
      rules: {
        num: [{ required: true, message: '请输入新增数量', trigger: ['blur'] }]
      }
    }
  },
  computed: {
    ...mapGetters([
      'currentSite'
    ])
  },

  created() {
  },

  methods: {

    // 取消
    close() {
      this.$emit('close')
    },

    // 确认
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            ...this.form,
            id: this.id
          }
          addCouponNumber(data).then(() => {
            this.$emit('confirm')
            this.$message.success('增加数量成功')
            this.loading = false
            this.close()
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }

}
</script>

<style lang="scss" scoped></style>
